#include "asgka_net.h"

#define ASGKA_CS_API extern "C" __declspec(dllexport)

void SUMapToChar(const std::map<std::string, u_long> &su, char* buf);
void CharToSVector(const char* buf, std::vector<std::string>& sv);

ASGKA_CS_API void ASGKASetup(const char* localname)
{
	std::string strlocalname(localname);
	asgka_setup(strlocalname);
}

ASGKA_CS_API void ASGKAMsgToWnd(void *wnd)
{
	asgka_msgToWnd((HWND)wnd);
}

ASGKA_CS_API void ASGKAClose()
{
	asgka_close();
}

ASGKA_CS_API void ASGKASearch()
{
	asgka_search();
}

ASGKA_CS_API void ASGKALeaveGroup(const char *groupname)
{
	std::string strgroupname(groupname);
	asgka_leaveGroup(strgroupname);
}

ASGKA_CS_API void ASGKAGetGroupMember(const char* groupname, char* buf)
{
	std::string strgroupname(groupname);
	std::map<std::string, u_long> temp = asgka_getGroupMember(groupname);
	SUMapToChar(temp, buf);
}

ASGKA_CS_API void ASGKACreateGroup(const char *groupname, const char* shsv)
{
	std::string strgroupname(groupname);
	std::vector<std::string> sv;
	CharToSVector(shsv, sv);
	asgka_createGroup(groupname, sv);
}

ASGKA_CS_API void ASGKAGetAllMember(char *buf)
{
	std::map<std::string, u_long> temp = asgka_getMemberList();
	SUMapToChar(temp, buf);
}

ASGKA_CS_API int ASGKAJoinGroup(const char *groupname, const char* leadername)
{
	std::string strgroupname(groupname);
	std::string strleadername(leadername);
	return  asgka_joinGroup(strgroupname, strleadername);
}

ASGKA_CS_API void ASGKAFree(void *buf)
{
	free(buf);
}

ASGKA_CS_API void ASGKAAddMember(const char *groupname, const char *membername)
{
	std::string strgroupname(groupname);
	std::string strmembername(membername);
	asgka_addMember(strgroupname, strmembername);
}

ASGKA_CS_API void ASGKAKickOut(const char *groupname, const char *membername)
{
	std::string strgroupname(groupname);
	std::string strmembername(membername);
	asgka_kickOut(strgroupname, strmembername);
}

ASGKA_CS_API void ASGKAGetGroupLeaderName(const char *groupname, char *namebuf)
{
	std::string strgroupname(groupname);
	std::string temp = asgka_getGroupLeaderName(strgroupname);
	memcpy(namebuf, temp.c_str(), temp.size());
}

void CharToSVector(const char* buf, std::vector<std::string>& sv)
{
	int i = 0;
	int length = *((int *)buf);
	i += 4;
	for (int k = 0; k < length; k++)
	{
		int nlen = *((int *)(buf + i));
		i += 4;
		std::string temp;
		temp.clear();
		temp.append(buf + i, nlen);
		sv.insert(sv.begin(), temp);
		i += nlen;
	}
}

void SUMapToChar(const std::map<std::string, u_long> &su, char* buf)
{
	std::string temp;
	temp.clear();
	int length = su.size();
	temp.append((char *)&length, 4);

	std::map<std::string, u_long>::const_iterator it = su.begin();
	for (; it != su.end(); it++)
	{
		int nlen = it->first.size();
		temp.append((char *)&nlen, 4);
		temp.append(it->first.c_str(), it->first.size());
		temp.append((char *)&it->second, 4);
	}

	memcpy(buf, temp.c_str(), temp.size());
}
